import requests
from baseTools.myLogger import logger
import json

class BaseAPI():

    def my_get(self, url, data, **kwargs):
        """
        get请求的封装
        :param url: 请求url
        :param data: 请求参数
        :param kwargs:
        :return: 响应的json数据
        """
        logger.info(f"请求接口：{url}，参数：{data}")
        try:
            rep = requests.get(url=url, params=data)
            logger.info(f"响应信息为：{json.dumps(rep.json())}")
            return rep.json()
        except Exception as e:
            logger.warning("接口请求失败，报错信息如下：\n", e)
            raise

    def my_post(self, url, params, data, **kwargs):
        """
        post请求的封装
        :param url: 请求url
        :param params: url参数
        :param data: 请求体参数
        :param kwargs:
        :return: 响应的json数据
        """
        logger.info(f"请求接口：{url}，鉴权：{params}，请求参数：{data}")
        try:
            rep = requests.post(url=url, params=params, json=data)
            logger.info(f"响应信息为：{json.dumps(rep.json())}")
            return rep.json()
        except Exception as e:
            logger.warning("接口请求失败，报错信息如下：\n", e)
            raise